package com.gamesnet.ihm;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import org.json.JSONException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.gamesnet.api.Api;
import com.gamesnet.api.Session;
import com.gamesnet.models.Commentaire;
import com.gamesnet.models.Listable;
import com.gamesnet.models.Publication;
import com.gamesnet.util.AdapterListener;
import com.gamesnet.widget.ActionBar;
import com.gamesnet.widget.ActionBar.IntentAction;
import com.gamesnet.widget.ListAdapter;

public class PublicationActivity extends Activity implements AdapterListener{
	
	Publication post;

	@Override
	public void onCreate(Bundle savedInstanceState) { 

		super.onCreate(savedInstanceState); 
		setContentView(R.layout.publication);

		final ActionBar actionBar = (ActionBar) findViewById(R.id.actionbarHaut);
		actionBar.setHomeAction(new IntentAction(this, new Intent(this, DashboardActivity.class), R.drawable.home));
		actionBar.setTitre(PageTitle.PUBLICATION);

		Bundle b = getIntent().getExtras();
		long postId = b.getLong("postId");

		try {
			post = Api.publication(String.valueOf(postId));

			ImageView avatar = (ImageView)findViewById(R.id.avatarPublication2);

			Bitmap bitmap = null;

			try {
				URL urlImage = new URL("http://10.0.2.2:8080/Gamesnet/images/upload/resized/avatar/"+post.image()+".jpg");
				HttpURLConnection connection = (HttpURLConnection) urlImage.openConnection();
				InputStream inputStream = connection.getInputStream();
				bitmap = BitmapFactory.decodeStream(inputStream);
				avatar.setImageBitmap(bitmap);
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}

			TextView expediteur = (TextView)findViewById(R.id.senderPublication2);
			TextView content = (TextView)findViewById(R.id.contentPublication2);

			expediteur.setText(post.getSender().afficher());
			content.setText(post.afficher());
			List<Commentaire>commentaires = post.getCommentaires();
			Log.v("Publication activity", "nb de comment : "+commentaires.size());
			ListAdapter<Commentaire> adapter = new ListAdapter<Commentaire>(this, commentaires);
			adapter.addListener(this);
			adapter.setUserId(Session.getInstance().getUser().getId());
			ListView list = (ListView)findViewById(R.id.listCommentaires);
			list.setAdapter(adapter);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void onClickText(Listable model, int position) {
		// TODO Auto-generated method stub

	}

	public void onClickSupp(Listable model, int position) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) { 
		super.onCreateOptionsMenu(menu);
		menu.add(0, 1, 0, "Commentez");
		return true; 
	}

	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case 1:
			Intent intent = new Intent(this, NewCommentaireActivity.class);
			intent.putExtra("postId", post.getId());
			startActivity(intent);
			return true;
		default:
			return true;
		} 
	}

}
